package service

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"holidayone/basic/config"
	"holidayone/handler/Request"
	"holidayone/handler/model"
	"holidayone/pkg"
	"math/rand"
	"net/http"
	"strconv"
	"time"
)

func SendSms(c *gin.Context) {
	var form Request.SendSmsS
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "参数错误",
		})
		return
	}
	count, _ := config.Rdb.Get(config.Ctx, "SendS"+form.Phones).Int64()
	if count >= 3 {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "验证码一分钟不能发送三次以上",
		})
		return
	}
	code := rand.Intn(900000) + 100000
	err, _ := pkg.SendSms(form.Phones, strconv.Itoa(code))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "验证码发送失败",
		})
		return
	}
	config.Rdb.Set(config.Ctx, "SendKey"+form.Phones, code, time.Hour*2)
	config.Rdb.Incr(config.Ctx, "SendS"+form.Phones)
	config.Rdb.Expire(config.Ctx, "SendS"+form.Phones, time.Minute*1)
	c.JSON(http.StatusOK, gin.H{
		"data": "200",
		"code": "验证码发送成功",
	})
	return
}

func Register(c *gin.Context) {
	var form Request.Register
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "参数错误",
		})
		return
	}
	var user model.User
	count, _ := config.Rdb.Get(config.Ctx, "SendKey"+form.UserName).Result()
	if count != form.SendSms {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "验证码不正确",
		})
		return
	}
	if err := config.DB.Where("user_name = ?", form.UserName).First(&user).Error; err == nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "该账号已注册，请直接登录",
		})
		return
	}
	user.UserName = form.UserName
	user.Password = pkg.Md5(form.Password)
	user.Nickname = form.Nickname
	user.Photo = form.Photo
	user.Signature = form.Signature
	err := user.Register(config.DB)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "注册失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": "200",
		"code": "注册成功",
	})
	return
}

func Login(c *gin.Context) {
	var form Request.Logion
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "参数错误",
		})
		return
	}
	var user model.User
	config.DB.Where("user_name = ?", user.UserName).Find(&user)
	if user.ID > 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "未注册不得,请注册",
		})
		return
	}
	if user.Password == pkg.Md5(form.Password) {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "密码输入错误,请重试",
		})
		return
	}

	token, err := pkg.GetJwtToken(form.UserName)
	if err != nil {
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data":  "200",
		"code":  "登录成功",
		"token": token,
	})
	return
}

func UserList(c *gin.Context) {
	var form Request.UserList
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "参数错误",
		})
		return
	}
	var user model.User

	list, err := user.UserSList(config.DB, form.UserName, form.Nickname)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "查询失败",
		})
		return
	}
	marshal, _ := json.Marshal(list)
	config.Rdb.Set(config.Ctx, "UserListKey", marshal, time.Hour*1)
	c.JSON(http.StatusOK, gin.H{
		"data":  "200",
		"code":  "查询成功",
		"token": marshal,
	})
	return
}

func FriendAdd(c *gin.Context) {
	var form Request.FriendAdd
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "参数错误",
		})
		return
	}
	var friends model.Friend
	friends.FriendId = form.FriendId
	friends.UserId = form.UserId
	friends.Reason = form.Reason
	friends.Status = form.Status
	err := friends.FriendAdd(config.DB)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "好友申请失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": "200",
		"code": "好友申请成功",
	})
	return
}

func FriendUpload(c *gin.Context) {
	var form Request.FriendUpdate
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "参数错误",
		})
		return
	}
	var friends model.Friend
	err := friends.FriendUpdate(config.DB, form.UserId, form.Status)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "好友申请验证失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": "200",
		"code": "好友申请验证成功",
	})
	return
}
func FriendList(c *gin.Context) {
	var form Request.FriendList
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "参数错误",
		})
		return
	}
	var friends model.Friend
	list, err := friends.FriendList(config.DB)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "好友列表查询失败",
		})
		return
	}
	marshal, _ := json.Marshal(list)
	config.Rdb.Set(config.Ctx, "FriendListKey", marshal, time.Hour*1)
	c.JSON(http.StatusOK, gin.H{
		"data": "200",
		"code": "好友列表查询成功",
		"list": list,
	})
	return
}
func BlackAdd(c *gin.Context) {
	var form Request.BlackAdd
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "参数错误",
		})
		return
	}
	var black model.Blacklist
	black.FriendId = form.FriendId
	black.UserId = form.UserId
	black.Status = form.Status
	err := black.BlackAdd(config.DB)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "好友黑名单添加失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": "200",
		"code": "好友黑名单添加成功",
	})
	return
}
func BlackList(c *gin.Context) {
	var form Request.BlacklistList
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "参数错误",
		})
		return
	}
	var friends model.Blacklist
	list, err := friends.BlacklistList(config.DB)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "黑名单列表查询失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": "200",
		"code": "黑名单列表查询成功",
		"list": list,
	})
	return
}
func CircleAdd(c *gin.Context) {
	var form Request.CircleAdd
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "参数错误",
		})
		return
	}
	var circle model.Circle
	circle.UserId = form.UserId
	circle.Text = form.Text
	err := circle.CircleAdd(config.DB)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "朋友圈发布失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": "200",
		"code": "朋友圈发布成功",
	})
	return
}
func CircleList(c *gin.Context) {
	var form Request.CircleList
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "参数错误",
		})
		return
	}
	var circle model.Circle
	list, err := circle.CircleList(config.DB)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"data": "400",
			"code": "朋友圈动态列表查询失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": "200",
		"code": "朋友圈动态列表查询成功",
		"list": list,
	})
	return
}
